package com.liangtian.mdm.system.taskass.service.impl;

import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.system.taskass.dao.TaskAssDao;
import com.liangtian.mdm.system.taskass.model.TaskAss;
import com.liangtian.mdm.system.taskass.service.TaskAssService;
import com.liangtian.mdm.system.ugroup.dao.SysUgroupUserDao;
import com.liangtian.mdm.util.ConstantBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaskAssServiceImpl implements TaskAssService {

    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @Autowired
    private TaskAssDao taskAssDao;

    @Autowired
    private SysUgroupUserDao sysUgroupUserDao;

    @Override
    public List<Map<String, Object>> selectTaskSurvey(TaskAss taskAss) {
        taskAss.setOwner(ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER).getUserId() + "");
        List<Map<String, Object>> surveyDiff = new ArrayList<Map<String, Object>>();
        List<String> surveyDiffLst = new ArrayList<String>();
        List<Map<String, Object>> users = taskAssDao.selectTaskSurvey(taskAss); //根据用户过滤
        for (Map user : users) {
            String busName = (String) user.get("busName");
            surveyDiffLst.add(busName);
        }
        List<String> groupIdLst = sysUgroupUserDao.queryGroupByUserId(ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER).getUserId() + "");
        for (String gId : groupIdLst) {
            taskAss.setOwner(gId);
            List<Map<String, Object>> groups = taskAssDao.selectTaskSurveyByGroup(taskAss); //根据用户组过滤
            if (users.size() > 0 && groups.size() > 0) {
                for (Map group : groups) {
                    String busName = (String) group.get("busName");
                    if (!(surveyDiffLst.contains(busName))) {
                        users.add(group);
                    }
                }
                surveyDiffLst.clear();
                for (Map user : users) {
                    String busName = (String) user.get("busName");
                    surveyDiffLst.add(busName);
                }
            } else if (0 == users.size() && groups.size() > 0) {
                users = groups;
            }

        }
        return users;
    }


    @Override
    public void insertTaskAss(TaskAss taskAss) {
        String[] busIds = taskAss.getBussid().split(",");
        String userId = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER).getUserId() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("owner", userId);
        taskAss.setOwner(userId);
        for (String busId : busIds) {
            map.put("bussId", busId);
            taskAss.setBussid(busId);
            String id = taskAssDao.queryTaskAssByBusId(map);
            if (null == id) { //没有这个业务类型和用户的代理
                taskAssDao.insertTaskAss(taskAss);
            } else { //已经存在这个用户和业务类型的代理
                taskAss.setId(id);
                taskAssDao.updateTaskAss(taskAss); //根据id更新
            }
        }

    }

    @Override
    public void restTaskAss(TaskAss taskAss) {
        String[] busIds = taskAss.getBussid().split(",");
        taskAss.setOwner(ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER).getUserId() + "");
        for (String busId : busIds) {
            taskAss.setBussid(busId);
            taskAssDao.restTaskAss(taskAss); //撤销
        }
    }

}
